/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.cli;

import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class PurgePidsAction implements Action {

    private static final Logger logger = Logger.getLogger(PurgePidsAction.class.getName());
    private final FedoraConnector con;
    private final File objectsFile;

    PurgePidsAction(FedoraConnector con, File objectsFile) {
        this.con = con;
        this.objectsFile = objectsFile;
    }

    @Override
    public void run() throws IOException {
        List<String> pidList = loadPids();
        for (String pid : pidList) {
            try {
                logger.log(Level.INFO, "purging {0}", pid);
                con.removeObject(pid, null);
            } catch (Throwable e) {
                logger.log(Level.SEVERE, "failed to purge object " + pid, e);
            }
        }
    }

    private List<String> loadPids() throws IOException {
        List<String> result = new ArrayList<String>();
        LineNumberReader reader = new LineNumberReader(new FileReader(objectsFile));
        while (reader.ready()) {
            String pid = reader.readLine();
            result.add(pid);
        }
        reader.close();
        return result;
    }
}
